<?php
header("Content-Type: text/html; charset=iso-8859-1");
require 'include.php';
if(!check_admin("commission_technique,commission_LN")) redirect('perso.php?action=admin');
else{
	$_SESSION['navigation'] = "calendrier_admin";
	$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "list";
	if($action == 'update'){
		$option = (!empty($_REQUEST['option'])) ? $_REQUEST['option'] : "prochains";
		$_SESSION['affichage'] = $option;
	}
	elseif($action == 'print'){
		$affichage = (!empty($_SESSION['affichage'])) ? $_SESSION['affichage'] : "";
		$table = new table();
		$table->filter = 1;
		$table->order = 'date';
		$table->styleclass = 'bold_hover';
		$fields = array("no","equipe","jour","date","heure","local","visiteur","resultat");
		$action = new table_field('action');
		$action->styleclass = 'nowrap';
		foreach($fields as $field){
			$$field = new table_field($field);
			$table->fields[] = $$field;
			if($field == 'no') $no->hidden = 1;
		}
		$table->fields[] = $action;
		// $resultat->styleclass = 'center';
		$id->hidden = 1;
		$table->nb_rows = 30;
		$q = "select matchs.no as no, salles.no as no_salle, local, visiteur, resultat,date,salles.nom as salle,equipe, jour, heure from matchs inner join equipes on matchs.equipe = equipes.nom left join salles on matchs.salle = salles.no order by date";
		$r = ExecRequete($q);
		$i = 0;
		while($s = LigneSuivante($r)){
		// 	$equipe = new equipe($id);
		// 	$saison = "";
		// 	$matchs = $equipe->matchs($saison);
		// #	$table->nb_rows += count($matchs);
		// 	$i = 0;
		// 	foreach($matchs as $match){
		   if(($affichage == "prochains" && $s->date >= date("Y-m-d")) ||
		   	($affichage == "resultat" && strlen($s->resultat) < 3 && $s->date <= date("Y-m-d")) ||
		   	($affichage == "tous")){
		   		$row = new table_row();
		   		$row->onclick = "";
		   		foreach($fields as $field){
		   			if($field == 'resultat'){
		   				if(strlen($s->resultat) == 3){
		   					$echo = format_resultat($s->resultat,$s->local,$s->visiteur);
		   					$tab_resultat = explode("-",$s->resultat);
		   					if(strstr(format_resultat($s->resultat,$s->local,$s->visiteur),"gagne") !== FALSE){
		   						$_SESSION['scores'][] = abs($tab_resultat[0]-$tab_resultat[1]);
		   					}
		   					else $_SESSION['scores'][] = -abs($tab_resultat[0]-$tab_resultat[1]);
							$_SESSION['label_scores'][] = (stripos($s->local,"LUTRY") !== FALSE) ? $s->visiteur : $s->local;
		   				}
		   				else{
							$test_score = ((strpos($s->equipe,'M15') !== FALSE) || (strpos($s->equipe,'M17') !== FALSE)||(strpos($s->equipe,'M19G') !== FALSE)) ? "this.value == '2-0' || this.value == '2-1' || this.value == '1-2' || this.value == '0-2' || this.value == '1-1'" : "this.value == '3-0' || this.value == '3-1' || this.value == '3-2' || this.value == '2-3' || this.value == '1-3' || this.value == '0-3'";
		   					$echo = "<input type = 'text' name = 'resultat_$s->no' size = '3' maxlength = '3' onkeyup = \"if($test_score) update_resultat('$s->no',this.value,'calendrier')\"/><span id = 'ok_$s->no' style = 'padding-left: 1em'></span>";
		   				}
		   			}
		   			elseif($field == 'salle'){
		   				$echo = str_ireplace("salle","",$s->salle);
		   			}
		   			else $echo = str_ireplace("vbc ","",$s->$field);
		   			$row->cols[$field] = $echo;
		   		}
		   		$key = key($table->rows) + 1;
		   		$row->cols['action'] = "<a href = 'javascript:form_edit_match(\"$s->no\",$i)'><img src = 'images/icon_edit.png' alt = 'edit' height = '14' width = '14' /></a><a href = 'javascript:if(confirm(\"Veuillez confirmer la suppression du match $s->no\")) del_match(\"$s->no\",$key);'><img src = 'images/icon_delete.png' alt = 'supp.' height = '14' width = '14' />";
		   		$table->rows[] = $row;
		   }
		   $i++;

			$footer = "";
		}
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer($footer);
	}
	else{
		entete('calendrier admin',"","",1);
		$_SESSION['affichage'] = 'resultat';
		$affichages = array("tous" => "tous les matchs", "prochains" => "prochains matchs","resultat" => "matchs sans r&eacute;sultat");

		echo "<h1>Calendrier des matchs</h1>";
		echo "<p class = 'center bold'>options d'affichage : ";
		echo "<select id = 'affichage_matchs' name = 'affichage' onchange = 'update_calendrier(this.options[this.selectedIndex].value)'>\n";
		foreach($affichages as $option => $print){
			$selected = ($_SESSION['affichage'] == $option) ? "selected = 'selected'" : "";
			echo "<option value = '$option' $selected>$print</option>\n";
		}
		echo "</select>\n";
		echo "</p>";
		echo "<p class = 'center'><button type = 'button' onclick = 'form_new_match(\"all\")'><img src = 'images/icon_new.png' alt = '' width = '14' height = '14' />nouveau match</button></p>\n";
		echo "<div id = 'calendrier'>\n";
		echo "</div>\n";
		echo "<script type = 'text/javascript'>print_calendrier()</script>\n";
		pied();
	}
}

?>
